package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.BizAccount;
import com.ruoyi.business.domain.BizAccountDetail;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BizPayOrderMapper;
import com.ruoyi.business.domain.BizPayOrder;
import com.ruoyi.business.service.IBizPayOrderService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 支付订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-09
 */
@Service
public class BizPayOrderServiceImpl implements IBizPayOrderService 
{
    @Resource
    private BizPayOrderMapper bizPayOrderMapper;

    @Resource
    private BizAccountServiceImpl bizAccountService;

    /**
     * 查询支付订单
     * 
     * @param id 支付订单ID
     * @return 支付订单
     */
    @Override
    public BizPayOrder selectBizPayOrderById(Long id)
    {
        return bizPayOrderMapper.selectBizPayOrderById(id);
    }

    /**
     * 查询支付宝表单
     *
     * @param id
     * @return form
     */
    @Override
    public String selectZfbForm(Long id)
    {
        return bizPayOrderMapper.selectZfbForm(id);
    }

    /**
     * 查询支付订单
     *
     * @param tradeNo 支付订单号
     * @return 支付订单
     */
    @Override
    public BizPayOrder selectBizPayOrderByTradeNo(String tradeNo)
    {
        return bizPayOrderMapper.selectBizPayOrderByTradeNo(tradeNo);
    }

    /**
     * 查询支付订单列表
     * 
     * @param bizPayOrder 支付订单
     * @return 支付订单
     */
    @Override
    public List<BizPayOrder> selectBizPayOrderList(BizPayOrder bizPayOrder)
    {
        return bizPayOrderMapper.selectBizPayOrderList(bizPayOrder);
    }

    /**
     * 新增支付订单
     * 
     * @param bizPayOrder 支付订单
     * @return 结果
     */
    @Override
    public int insertBizPayOrder(BizPayOrder bizPayOrder)
    {
        bizPayOrder.setCreateTime(DateUtils.getNowDate());
        return bizPayOrderMapper.insertBizPayOrder(bizPayOrder);
    }

    /**
     * 修改支付订单
     * 
     * @param bizPayOrder 支付订单
     * @return 结果
     */
    @Override
    public int updateBizPayOrder(BizPayOrder bizPayOrder)
    {
        bizPayOrder.setUpdateTime(DateUtils.getNowDate());
        return bizPayOrderMapper.updateBizPayOrder(bizPayOrder);
    }

    /**
     * 删除支付订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBizPayOrderByIds(String ids)
    {
        return bizPayOrderMapper.deleteBizPayOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除支付订单信息
     * 
     * @param id 支付订单ID
     * @return 结果
     */
    @Override
    public int deleteBizPayOrderById(Long id)
    {
        return bizPayOrderMapper.deleteBizPayOrderById(id);
    }

    /**
     * 支付回调订单状态修改
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public void notifyPayOrder(BizPayOrder payOrder)
    {
        updateBizPayOrder(payOrder);

        bizAccountService.accountChange(payOrder.getMemberId(), BizAccount.DOU_BALANCE, BizAccountDetail.DOU_DETAIL_TYPE_CHARGE, payOrder.getTradeNum().longValue(), payOrder.getTradeNo(), BizAccountDetail.DOU_DESC_CHARGE);
    }
}
